var player = {} //玩家信息
//var propName = null
var itemsById = []
var template = baidu.template;
var latticeIndex = 0
var treasureFlag = 0
//var templateConfig = null
var token = null
var userId = null
var userServerClient = createClient()
var client = createClient()
var windowHeight = 0
var windwoWidth = 0
var font_size = 0
var heartbeatTimer = null //心跳
$(document).ready(function() {
  windowHeight = $(window).height()
  windwoWidth = $(window).width()
  loadLoginMain()
  font_size = parseInt($('body').css('font-size').replace('px',''))

})

function exit() {
  $('.exit').click(function() {
    $('.append').remove()
    $('#mainContent').removeClass('bigMainContent')
    $('#messageMain').removeClass('litterMessageMain')
  })
}

var msgLen = 0
var msgQueue = []
setInterval(function() {
  if (msgQueue.length > 0) {
    var msg = msgQueue.shift()
    $('#messageMain .message').prepend('<div class="msg">' + msg + '</div>')
    msgLen += 1
    if (msgLen > 20) {
      $('#messageMain .message .msg:last').remove()
      msgLen -= 1
    }
  }
}, 500)

function showMessage(msg) {
  msgQueue.push(msg)
}

//初始化背包
function initPackag(cb) {
  request.getBagInfo(function(res) {
    player.bag = res.bag
    player.basicInfo.money = res.money
    player.basicInfo.gold = res.gold
    var items = player.bag.items
    itemsById = []
    for (var i in items) {
      var itemComponent = items[i]
      if (!itemComponent) {
        continue
      }
      var templateId = itemComponent.templateId
      items[i] = itemComponent
      if (itemsById[templateId]) {
        itemsById[templateId].push(itemComponent)
      } else {
        itemsById[templateId] = [itemComponent]
      }
    }
    if (cb)
      cb()
  })
}

//获取某一物品的总数量
function getTotalNumByItemId(itemId) {
  var itemInfoId = itemsById[itemId]

  //查找物品是否已经存在
  if (!itemInfoId) {
    return 0
  }

  var total = 0
  for (var i in itemInfoId) {
    total += itemInfoId[i].stack
  }
  return total
}


//获取物品类型
function getItemMainType(templateId) {
  return Math.floor(templateId / 10000)
}

//获取物品子类型
function getItemSubType(templateId) {
  return Math.floor(templateId % 10000 / 100)
}

//去掉字符串前后空格
function Trim(str) {
    return str.replace(/(^\s*)|(\s*$)/g, "")
}
//弹出窗口
function popOpen(msg, cancel, confirm) {
  popClose()
  $('#main').append(template('t_pop'))
  $('#popBox .content').html(msg)
  if (cancel) {
    $('#popBox .cancel').html(cancel)
  } else {
    $('#popBox .cancel').css('display', 'none')
  }

  if (confirm) {
    $('#popBox .confirm').html(confirm)
  }

  $('#popBg, #popBox .cancel').click(function() {
    popClose()
  })

  $('#popBox .confirm').click(function() {
    request.triggerEvent(function(res) { //触发事件
      popClose()
      if (res.error === 0) {
        if (res.eventType === 2) {
          if (res.propId) {
            showMessage('在山洞中探索后发现了' + propName[res.propId])
          }
          if (res.hurt) {
            showMessage('被山洞顶部掉落的石块砸伤,损失了' + res.hurt + '点生命')
          }
        } else if (res.eventType === 3) {
          if (res.propId) {
            showMessage('扯断蔓藤后在下面发现了' + propName[res.propId])
          }

          if (res.hurt) {
            showMessage('原来是石头反射的光,不慎被划伤损失' + res.hurt + '点生命')
          }
        } else if (res.eventType === 4) {
          if (res.recoverHp) {
            showMessage('清凉的泉水为身体注入了新的活力,恢复' + res.recoverHp + '生命')
          }
          if (res.hurt) {
            showMessage('泉水中竟然有毒草的汁液!损失了' + res.hurt + '点生命')
          }
        } else if (res.eventType === 5) {
          if (res.propId) {
            showMessage('砸开瓦罐后,幸运的在里面发现了' + propName[res.propId])
          } else {
            showMessage('费了好大力气才砸开了罐子,却什么都没发现')
          }
        } else if (res.eventType === 6) {
          if (res.luckyBuff) {
            showMessage('母兽感受到你的善意,获得它的祝福,幸运提升' + res.luckyBuff)
          }
          if (res.hurt) {
            showMessage('母兽为保护孩子向你发起攻击,损失了' + res.hurt + '点生命')
          }
        } else if (res.eventType === 7) {
          if (res.propId) {
            showMessage('成功战胜了怪兽,并获得了战利品' + propName[res.propId])
          }
          if (res.hurt) {
            showMessage('怪兽临死的反扑异常凶猛，损失了' + res.hurt + '点生命')
          }
        } else if (res.eventType === 8) {
          if (res.propId) {
            showMessage('为了感激你的帮助,冒险者将' + propName[res.propId] + '赠送给了你')
          } else {
            showMessage('冒险者向你表达了谢意之后便离开了')
          }
        } else if (res.eventType === 9) {
          if (res.stamina) {
            showMessage('在帐篷中小憩了一下,恢复了' + res.stamina + '体力')
          }
        } else if (res.eventType === 10) {
          showMessage('从冒险商人手上购买了治疗药剂')
        } else if (res.eventType === 11) {
          if (res.propId) {
            showMessage('赌博总会有机会获得好东西的.获得了' + propName[res.propId])
          }
        } else if (res.eventType === 12) {
          if (res.luckyBuff) {
            showMessage('对守护神的真诚祈祷为你带来了祝福.幸运提升' + res.luckyBuff)
          }
        }
      } else if (res.error === 3) {
        showMessage('银币不足')
      } else if (res.error === 5) {
        showMessage('金币不足')
      } else if (res.error === 6) {
        showMessage('治疗药剂不足')
      } else if (res.error === 4) {
        showMessage('体力太少,无法做多余行动')
      }
    })
  })
}

function popClose() {
  $("#popBg, #popBox").remove()
}

//弹出窗口
function errorPopOpen(msg) {
  errorPopClose()
  $('#main').append(template('t_errorPop'))
  $('#errorPopBox .content').html(msg)

  $('#errorPopBg, #errorPopBox').click(function() {
    errorPopClose()
  })
}

function errorPopClose() {
  $("#errorPopBg, #errorPopBox").remove()
}
